﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pout_Porri.Objects.Interface;
using Pout_Porri.Utils;

namespace Pout_Porri.Objects
{
    public enum DanteStates
    {
        running,
        attacking,
        stand,
        jumping,
        falling
    }

    class PlayerExample : PhysicsGameEntity, IGameEntity
    {
        protected DanteStates currentState;


        private int maxJumps = 2;

        protected int MaxJumps
        {
            get { return maxJumps; }
            set { maxJumps = value; }
        }
        

        private int contJumps = 0;
        
        private float jumpHeight = 25;

        protected float JumpHeight    
        {
            get { return jumpHeight; }
            set { jumpHeight = value; }
        }

        public PlayerExample(Texture2D image, Vector2 position, Vector2 velocity)
            :base(image, position, velocity, false)
        {
            DefaultVelocityX = velocity.X;
            this.currentState = DanteStates.stand;
            this.configAnimations();
        }

        public void configAnimations()
        {
            //Animation danteStand = new Animation("stand",new Vector2(37, 68), 250, 0, 0, 4, Axis.X);
            //Animation danteRunnig = new Animation("running",new Vector2(51, 68), 100, 0, 1, 7, Axis.X);
            Animation danteStand = new Animation("stand", new Vector2(32, 32), 250, 0, 0, 4, Axis.X);
            Animation danteRunnig = new Animation("running", new Vector2(32, 32), 100, 0, 1, 7, Axis.X);

            Animation danteJumping = new Animation("jumping",new Vector2(51, 68), 250, 0, 2, 2, Axis.X);
            Animation danteFalling = new Animation("falling",new Vector2(51, 68), 250, 3, 2, 2, Axis.X);
            Animation danteAttacking = new Animation("attacking",new Vector2(51, 68), 250, 0, 0, 4, Axis.X);

            this.addAnimation(danteStand);
            this.addAnimation(danteRunnig);
            this.addAnimation(danteJumping);
            this.addAnimation(danteFalling);
            this.addAnimation(danteAttacking);

            this.setCurrentAnimation("stand");
        }

        public void handleInput()
        {
            DanteStates pState = currentState;

            direction.X = 0;
            if (currentState != DanteStates.jumping && currentState != DanteStates.falling)
                pState = DanteStates.stand;
            
            if (Game1.keyboardState.IsKeyDown(Keys.Right))
            {
                this.deFlipHorizontally();
                direction.X = 1;
                if (currentState != DanteStates.jumping && currentState != DanteStates.falling)
                    pState = DanteStates.running;

            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Left))
            {
                this.flipHorizontally();
                direction.X = -1;
                if (currentState != DanteStates.jumping && currentState != DanteStates.falling)
                    pState = DanteStates.running;
            }

            if (Game1.keyboardState.IsKeyDown(Keys.Up) && !Game1.previusKeyboardState.IsKeyDown(Keys.Up))
            {
                if (contJumps < maxJumps)
                {
                    contJumps++;
                    this.Velocity = new Vector2(this.Velocity.X, -jumpHeight);
                    this.setGrounded(false);
                }
            }
            
            if (pState != currentState)
            {
                currentState = pState;
            }
        }

        public override void setGrounded(bool b)
        {
            base.setGrounded(b);
            if (b == true)
            {
                contJumps = 0; // reset the jumps
            }
        }

        
        
        public override void update(GameTime gameTime)
        {
            handleInput();

            Game1.camera.Update(this.position - Game1.camera.Position);
            this.applyMovement(gameTime);
            checkMatrixCollisionInY();

            #region animation

            if (currentState == DanteStates.stand)
            {
                this.setCurrentAnimation("stand");
            }
            else if (currentState == DanteStates.running)
            {
                this.setCurrentAnimation("running");
            }
            else if (currentState == DanteStates.jumping)
            {
                this.setCurrentAnimation("jumping");
            }
            else if (currentState == DanteStates.falling)
            {
                this.setCurrentAnimation("falling");
            }

            #endregion

            base.update(gameTime);
        }
    }
}
